package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.pojo.Cart;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.bean.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;
    //新增购物车 重定向到新增成功界面 将购物车信息携带过去
    @GetMapping
    public String saveCart(Cart cart){
        cartService.saveCart(cart);
        return "redirect:http://cart.gmall.com/addCart.html?skuId="+cart.getSkuId()+"&count="+cart.getCount();
    }

    // 根据userId查询查询已选中的购物车记录
    @GetMapping("user/{userId}")
    @ResponseBody
    public ResponseVo<List<Cart>> queryCheckedCartByUserId(@PathVariable("userId")Long userId){
        List<Cart> carts = cartService.queryCheckedCartByUserId(userId);
        return ResponseVo.ok(carts);
    }
    @GetMapping("addCart.html")
    public String addCart(Cart cart, Model model){

        //本次新增数量
        BigDecimal count = cart.getCount();

        //根据skuId查询购物车记录

        cart = cartService.queryCart(cart.getSkuId());
        cart.setCount(count);
        model.addAttribute("cart",cart);
        return "addCart";
    }

    //查询购物车
    @GetMapping("cart.html")
    public String queryCart(Model model){
        List<Cart> carts = cartService.queryCarts();
        model.addAttribute("carts",carts);
        return "cart";
    }

    //更新购物车数量
    @PostMapping("updateNum")
    @ResponseBody
    public ResponseVo updateNum(@RequestBody Cart cart){
        cartService.updateNum(cart);
        return ResponseVo.ok();
    }

    //更新选中状态
    @PostMapping("updateStatus")
    @ResponseBody
    public ResponseVo updateStatus(@RequestBody Cart cart){
        cartService.updateStatus(cart);
        return ResponseVo.ok();
    }

    //跟进skuId删除购物车记录
    @PostMapping("deleteCart")
    @ResponseBody
    public ResponseVo deleteCart(@RequestParam("skuId")String skuId){
        cartService.deleteCart(skuId);
        return ResponseVo.ok();
    }

    @GetMapping("test")
    @ResponseBody
    public String test(){
//        System.out.println("hello test");
////        System.out.println("userId="+request.getAttribute("userId"));
////        System.out.println("userKey="+request.getAttribute("userKey"));
//        System.out.println("userInfo="+LoginInterceptor.getUserInfo());
        long now = System.currentTimeMillis();
//        try {
//
            cartService.execute1();
            cartService.execute2();
//
//            //获取返回结果集 包括正常和异常
//            future1.addCallback(result->{
//                //获取正常返回结果集
//                System.out.println("execute1的正常结果集"+result);
//            },ex->{
//                System.out.println("execute1的异常信息"+ex);
//            });
//
//            future2.addCallback(result -> {
//                System.out.println("execute2的正常结果集"+result);
//            },ex -> {
//                System.out.println("execute2的异常信息"+ex);
//            });
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        System.out.println("执行时间:"+(System.currentTimeMillis()-now));
        return "hello test";
    }
}
